package com.huawei.chengdu.meetingdemo.view.dialogs;

import android.content.Context;
import android.os.SystemClock;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Chronometer;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.device.idea.meeting.CallCtrl.model.CallStatus;
import com.device.idea.meeting.ConfCtrl.model.ActiveConfInfo;
import com.huawei.chengdu.meetingdemo.consts.ViewNameConts;
import com.huawei.chengdu.meetingdemo.util.LogUtil;
import com.huawei.chengdu.meetingdemo.presenter.MeetingManager;
import com.huawei.chengdu.meetingdemo.R;

public class MeetingInfoView extends BaseView {
    private static final String TAG = "MeetingInfoView";

    private CallStatus callStatus; // 会议信息

    private ActiveConfInfo activeConfInfo; // 会议高级信息

    private TextView confTheme;

    private TextView confId;

    private TextView leftTime;

    private TextView hostPwd;

    private TextView guestPwd;

    private CheckBox cbGuestPwd;

    private CheckBox cbHostPwd;

    private LinearLayout meetingMoreLl;

    private Chronometer confEnterTime;

    private RelativeLayout advMeetingInfo;

    private RelativeLayout hostPwdRl;

    private RelativeLayout guestPwdRl;

    private RelativeLayout confIdRl;

    private LinearLayout screenLayout;

    private FrameLayout fullLayout;

    private RelativeLayout tiemRl;

    public MeetingInfoView(Context context) {
        super(context);
        init();
        initView();
        initData();
        initEvent();
    }

    private void init() {
        callStatus = MeetingManager.getInstance().getCallStatus();
    }

    private void initView() {
        if (mRootView == null) {
            LogUtil.error("initView error, mRootView is null");
            return;
        }
        confTheme = mRootView.findViewById(R.id.tv_conf_theme_detail);
        confId = mRootView.findViewById(R.id.tv_conf_id_detail);
        confEnterTime = mRootView.findViewById(R.id.tv_enter_meeting_time_detail);

        advMeetingInfo = mRootView.findViewById(R.id.rl_adv_meeting_info);
        advMeetingInfo.setOnClickListener(view -> clickConfAdv());

        screenLayout = mRootView.findViewById(R.id.ll_conf_info);
        fullLayout = mRootView.findViewById(R.id.adv_info_fullScreenLayout);
        confIdRl = mRootView.findViewById(R.id.conf_id_rl);

        leftTime = mRootView.findViewById(R.id.tv_left_time);
        hostPwd = mRootView.findViewById(R.id.tv_host_pwd);
        guestPwd = mRootView.findViewById(R.id.tv_guest_pwd);
        cbHostPwd = mRootView.findViewById(R.id.cb_host_pwd);
        cbGuestPwd = mRootView.findViewById(R.id.cb_guest_pwd);
        meetingMoreLl = mRootView.findViewById(R.id.meeting_more_ll);

        hostPwdRl = mRootView.findViewById(R.id.rl_host_pwd);
        guestPwdRl = mRootView.findViewById(R.id.rl_guest_pwd);
        tiemRl = mRootView.findViewById(R.id.rl_time);

        if (MeetingManager.getInstance().isJoinConfStateP2P()) {
            confIdRl.setVisibility(View.GONE);
            meetingMoreLl.setVisibility(View.GONE);
        } else {
            confIdRl.setVisibility(View.VISIBLE);
            meetingMoreLl.setVisibility(View.VISIBLE);
        }
    }

    private void initData() {
        confTheme.setText(callStatus.getConferenceName());
        confId.setText(callStatus.getConferenceId());
        if (MeetingManager.getInstance().isMeChairman()) {
            int leftTimeValue = MeetingManager.getInstance().getConfLeftTime();
            leftTime.setText(leftTimeValue <= 0
                    ? mContext.getString(R.string.confinfo_permanent_meeting)
                    : leftTimeValue + mContext.getString(R.string.min));
        }
        confEnterTime.setBase(SystemClock.elapsedRealtime() - callStatus.getJoinTime() * 1000);
        confEnterTime.start();

        tiemRl.setVisibility(MeetingManager.getInstance().isMeChairman() ? View.VISIBLE : View.GONE);

        if (activeConfInfo == null) {
            return;
        }

        if (TextUtils.isEmpty(activeConfInfo.getChairPwd())) {
            hostPwdRl.setVisibility(View.GONE);
        } else {
            hostPwdRl.setVisibility(View.VISIBLE);
            hostPwd.setText(activeConfInfo.getChairPwd());
            setPwdShowType(hostPwd, false);
        }

        if (TextUtils.isEmpty(activeConfInfo.getGuestPwd())) {
            guestPwdRl.setVisibility(View.GONE);
        } else {
            guestPwdRl.setVisibility(View.VISIBLE);
            guestPwd.setText(activeConfInfo.getGuestPwd());
            setPwdShowType(guestPwd, false);
        }

    }

    private void initEvent() {
        cbHostPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setPwdShowType(hostPwd, isChecked);
            }
        });
        cbGuestPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setPwdShowType(guestPwd, isChecked);
            }
        });
    }

    private void setPwdShowType(TextView textView, boolean isPwdClearText) {
        if (isPwdClearText) { // 设置为可见的密码
            textView.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        } else { // 设置为不可见的密码
            textView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    }

    /**
     * 回到会议信息页面
     */
    public void backToSetting() {
        LogUtil.info("backToSetting");
        screenLayout.setVisibility(View.VISIBLE);
        fullLayout.removeAllViews();
        fullLayout.setVisibility(View.GONE);
    }

    /**
     * 加载全屏的高级会议信息界面。
     *
     * @param baseView 传入view
     */
    public void loadFullWorkArea(BaseView baseView) {
        screenLayout.setVisibility(View.GONE);
        fullLayout.setVisibility(View.VISIBLE);
        fullLayout.removeAllViews();
        fullLayout.addView(baseView.getLayoutView());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.meeting_info_view;
    }

    @Override
    public void viewDataChanged(int eventId, Object obj) {
    }

    @Override
    public void unRegisterListenService() {
        confEnterTime.stop();
    }

    @Override
    public void registerListenerService() {
    }

    private void clickConfAdv() {
        // 多流会议待补充
        // 单流会议
        if (ViewsFactory.getInstance().isViewExist(ViewNameConts.ADV_CONF_INFO_VIEW)) {
            ViewsFactory.getInstance().removeView(ViewNameConts.ADV_CONF_INFO_VIEW);
        }

        BaseView baseView = ViewsFactory.getInstance().getBaseView(mContext, ViewNameConts.ADV_CONF_INFO_VIEW, true);
        if (baseView instanceof AdvMeetingInfoView) {
            AdvMeetingInfoView advConfInfoView = (AdvMeetingInfoView) baseView;
            advConfInfoView.initData();
            loadFullWorkArea(advConfInfoView);
        }
    }
}
